package com.sfeir.client.tetriselement;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public final class TetrisObjectFactory {

	private final TetrisObject SQUARE;
	private final TetrisObject S;
	private final TetrisObject L;
	private final TetrisObject INVERSED_L;
	private final TetrisObject INVERSED_S;
	private final TetrisObject T;
	private final TetrisObject BAR;

	private final List<TetrisObject> objects = new ArrayList<TetrisObject>();
	private final Map<Integer, TetrisObject> tetrisElements = new HashMap<Integer, TetrisObject>();

	public TetrisObjectFactory(com.sfeir.canvas.util.client.ResourceManager manager) {
		this.SQUARE = new SquareObject(manager);
		this.S = new SObject(manager);
		this.L = new LObject(manager);
		this.INVERSED_L = new InversedLObject(manager);
		this.INVERSED_S = new InversedSObject(manager);
		this.T = new TObject(manager);
		this.BAR = new BarObject(manager);
		
		this.tetrisElements.put(1, SQUARE);
		this.tetrisElements.put(2, S);
		this.tetrisElements.put(3, L);
		this.tetrisElements.put(4, INVERSED_S);
		this.tetrisElements.put(5, INVERSED_L);
		this.tetrisElements.put(6, T);
		this.tetrisElements.put(7, BAR);
	}

	/**
	 * Method allowing to get the next Tetris Object
	 * 
	 * @return
	 */
	public TetrisObject nextObject() {
		TetrisObject tetrisObject = null;
		if (objects.size() > 0) {
			tetrisObject = objects.get(0);
			objects.remove(0);
		}
		return tetrisObject;
	}

	/**
	 * Method allowing to initialize the sequence of the Tetris Elements that
	 * will be returned by the Factory
	 */
	public void initFactorySequence(List<Integer> sequence) {
		objects.clear();
		for (Integer tetrisElementId : sequence) {
			objects.add(tetrisElements.get(tetrisElementId));
		}
	}

	/**
	 * Method allowing to return the Tetris Element associated to a given Id
	 * 
	 * @param id
	 * @return
	 */
	public TetrisObject getTerisElementById(Integer id) {
		if (tetrisElements.containsKey(id)) {
			return tetrisElements.get(id);
		}
		return null;
	}
	
	/**
	 * 
	 * @return
	 */
	public boolean hasMoreTetrisElement() {
		return (objects.size() > 0);
	}
	
	public int getRemainingPieces() {
		return objects.size();
	}

}
